//
//  QLTabPageIndicator.m
//  QLSchool_IOS
//
//  Created by anan on 14-1-15.
//  Copyright (c) 2014年 baobaodev_anan. All rights reserved.
//

#import "QLTabPageIndicator.h"

@implementation QLTabPageIndicator

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setup];
    }
    return self;
}

- (void)setup
{
    
    _paddingHorizontal = 10;
    
    _btn_bg = [UIColor clearColor];
    _colorStateNormal = [UIColor blackColor];
    _colorStateSelected = COLOR(245, 65, 64, 1.0);
    _colorStateHighlighted = [UIColor colorWithRed:0.914 green:0.435 blue:0.008 alpha:1.000];
//    UIImage *imgBox = [UIImage imageNamed:@"viewpager_title_b"];
    
//    imgBox = [imgBox resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 20, 10) resizingMode:UIImageResizingModeStretch];
    
//    _buttonStateNormal = [UIImage imageNamed:@"viewpager_title_a"];
//    _buttonStateHighlighted = imgBox;
//    _buttonStateSelected = imgBox;
    if (self.textFontSize==0) {
        _textFontSize = 15;
    }
    
    _textFont = [UIFont systemFontOfSize:_textFontSize];
       _tabPageButtons = [[NSMutableArray alloc]init];
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.bounces = NO;
}

- (void)reloadData
{
    for (QLTabPageButton *button in self.tabPageButtons) {
        [button removeFromSuperview];
    }
    [self.tabPageButtons removeAllObjects];
    if (!self.titles||[self.titles count]==0) {
        return;
    }
    if (self.minWidth<=0) {
        self.minWidth = self.frame.size.width/self.titles.count;
    }
    int lastX = 0;
    for (NSString *title in self.titles) {
        CGSize size = CGSizeMake(1000,self.frame.size.height);
        //计算实际frame大小，并将label的frame变成实际大小
        CGSize labelsize = [title sizeWithFont:self.textFont constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
        QLTabPageButton  *btns = [[[NSBundle mainBundle ]loadNibNamed:@"QLTabPageButton" owner:self options:nil ]lastObject ];
        if (self.isTransparent) {
            btns.backgroundColor =[UIColor clearColor];
        }else {
            btns.backgroundColor =[UIColor clearColor];
        }
       
        int w = labelsize.width + self.paddingHorizontal * 2+5;
        if (w<self.minWidth) {
            w = self.minWidth;
        }
        CGRect rect = CGRectMake(lastX, 0, w, self.frame.size.height);
        lastX = lastX + w;
        btns.frame = rect;
        btns.hotBtn.frame = CGRectMake(0, 0, w, self.frame.size.height);
        
        [btns.hotBtn setTitleColor:_colorStateNormal forState:UIControlStateNormal];
        [btns.hotBtn setTitleColor:_colorStateSelected forState:UIControlStateSelected];
        [btns.hotBtn setTitleColor:_colorStateHighlighted forState:UIControlStateHighlighted];
        [btns.hotBtn setTitle:title forState:UIControlStateNormal];
        
        btns.hotBtn.titleLabel.font = [UIFont systemFontOfSize:_textFontSize];
        [btns.hotBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btns];
        btns.hotBtn.tag = [self.tabPageButtons count];
        
        [self.tabPageButtons addObject:btns];
    }
    self.contentSize = CGSizeMake(lastX, self.frame.size.height);
    self.select = 0;
    //self.backgroundColor = [UIColor clearColor];
}

- (void)buttonAction:(id)sender
{
    int index = (int)((UIButton*)sender).tag;
    if (self.viewPager) {
        self.viewPager.currentIndex = index;
        self.select = index;
    }
}

- (void)setSelect:(int)select
{
    _select = select;
    
    for (QLTabPageButton *btn in self.tabPageButtons) {
        btn.hotBtn.selected = NO;
        btn.hotBtn_bg.hidden= YES;
    }

    QLTabPageButton *b =[self.tabPageButtons objectAtIndex:select];
    b.hotBtn_bg.hidden = NO;
    b.hotBtn.selected = YES;
    //================================================//
    //这里处理滚动到中间
    int width = self.contentSize.width;
    int pWidth = self.frame.size.width;
    if (width<=pWidth) {
        return;
    }
    //把当前选中的button移到中间
    int offsetX = b.frame.origin.x+b.frame.size.width/2-pWidth/2;
    if (offsetX<0) {
        offsetX = 0;
    }
    else if (offsetX>width-pWidth) {
        offsetX = width-pWidth;
    }
    
    self.contentOffset = CGPointMake(offsetX, 0);
}

@end
